package servlet;

import com.alibaba.fastjson.JSON;
import dao.UserDao;
import dao.impl.UserDaoImpl;
import pojo.entity.User;
import util.JDBCDruidUtil;
import util.Result;
import util.SystemCode;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 接口
 *
 */
@WebServlet("/user")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1.编码方式
//        2.处理请求信息
        String username = req.getParameter("username");
        String password = req.getParameter("password");
//        3.连接数据库
        UserDao dao = new UserDaoImpl();
        boolean right = dao.loginByUsernameAndPwd(new User(username, password));
        JDBCDruidUtil.close();
//        4.响应
        Result result = null;
        if(right){
            result = Result.success("登录成功！");
        }else{
            result = Result.error(SystemCode.USERNAME_ERROR.getCode(),SystemCode.USERNAME_ERROR.getMessage(),null);
        }
        resp.getWriter().write(JSON.toJSONString(result));


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
